﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eDream.admin
{
    public partial class Lich_su_nap_tien : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int phi = getPhidong();
                int tiendong = getSotien();
                if (tiendong > 0 && tiendong < phi)
                    panl.Visible = true;
                else
                    panl.Visible = false;
            }
        }

        protected void btnNaptien_Click(object sender, EventArgs e)
        {
            int sotien = int.Parse(txt.Text);
            int tiendong = getSotien();
            if (sotien > getPhidong())
                lbthongbao.Text = "Số tiền của bạn đã lớn hơn mức phí quy định";
            else if ((sotien + tiendong) > getPhidong())
                lbthongbao.Text = "Tổng số tiền của bạn đã lớn hơn mức phí quy định";
            else
            {
                string id = Request.QueryString["k"].ToString();
                DataClasses1DataContext dx = new DataClasses1DataContext();
                try
                {
                    dx.Connection.Open();
                    //dx.spUdateMoney(int.Parse(id), sotien, 0, 0);
                   // dx.SubmitChanges();
                    lbthongbao.Text = "";
                    if (getSotien() < getPhidong())
                        panl.Visible = true;
                    else
                        panl.Visible = false;
                }
                catch (Exception) { }
                finally { dx.Connection.Close(); }
            }
        }

        int getPhidong()
        {
            int phidong = 0;
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                var list = from k in dx.config_mucHoahongs
                           where k.ID == "Thuphi"
                           select k;
                foreach (config_mucHoahong cf in list)
                    phidong = (int)cf.Value;

            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
            return phidong;
        }

        int getSotien()
        {
            int phidong = 0;
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                string id = Request.QueryString["k"].ToString();
                dx.Connection.Open();
                var list = from k in dx.UserDBs
                           where k.ID == int.Parse(id)
                           select k;
                foreach (UserDB cf in list)
                {
                    phidong = (int)cf.money;
                    if (!IsPostBack)
                    {
                        lbname.Text = "(" + cf.UserName + ")" + cf.Lastname + " " + cf.FirtName;
                    }
                }
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
            return phidong;
        }
    }
}